/*
 * Josh Lindoo
 * Login ID : lind6441
 * CS-102, Winter 2013
 * Programming Assignment 2
 * Prog2 class: Controls the program flow of the assignment
 */

import java.io.*;
import java.util.Scanner;

public class Prog2 {
	
	private static MovieList movieList = new MovieList();
	
	/*
	 * Method: main
	 * Purpose: Control the program flow and create database obj.
	 * 			to store movies and showings
	 * Parameters:
	 * 		String[] args:	used to input the movie/showing file names
	 * Returns: void
	 */
	
	public static void main(String[] args) {
		
		Scanner console = new Scanner(System.in);	// Keyboard Input
		Scanner moviesFile = null;					// File Scanner
		Scanner showingsFile = null;				// File Scanner
		
		//try the movies file
		try {
			moviesFile = new Scanner(new File(args[0]));
		}
		//if there is no parameter for showings file
		catch (ArrayIndexOutOfBoundsException exc) { 
			//if there is nothing at args[0], there weren't any parameters
			System.out.println("No arguments were passed in for the movies " +
					"and showings files.");
			System.exit(0); 
		}
		//if the file doesn't exist or it fails to open
		catch (FileNotFoundException exc) {
			System.out.println("First file from parameters (movie list) " +
					"could not be opened.");
			System.exit(0);
		}
		
		//try the showings file
		try {
			showingsFile = new Scanner(new File(args[1]));
		}
		//if there is no parameter for showings file
		catch (ArrayIndexOutOfBoundsException exc) {
			System.out.println("No parameter entered for showings list " +
					"file name.");
			System.exit(0);
		}
		//if the file doesn't exist or it fails to open
		catch (FileNotFoundException exc) {
			System.out.println("Second file from parameters (showings list) " +
					"could not be opened.");
			System.exit(0);
		}
		
		storeMovies(moviesFile); //store all of the movies
		storeShowings(showingsFile); //store all of the showings
		movieList.setShowingTitles(); //match the showings to the movies
		
		while(true) {
			menu(console); //call the menu
		}
	}
	
	/*
	 * Method: menu
	 * Purpose: Print the menu and handle the user's selections
	 * Parameters:
	 * 		Scanner console:	scanner of user input
	 * Returns: void
	 */
	
	public static void menu(Scanner console) {
		int selected = 0; //stores menu selection
		
		final int PRINT_ALL = 1; //const for print
		final int SEARCH = 2; //const for search
		final int EXIT = 0; //const for exit
		
		//Print the menu'
		System.out.println("\n------------------------------------");
		System.out.println("Enter one of the following commands:");
		System.out.println("------------------------------------");
		System.out.println(PRINT_ALL + ") Print movies and showings");
		System.out.println(SEARCH + ") Search for a movie");
		System.out.println(EXIT + ") Exit");
		System.out.print("\nInput >> ");
		
		if(console.hasNextInt()) { //check to see if there is input of type int
			selected = console.nextInt();
			console.nextLine(); //Moves the pointer to a new line
			
			switch (selected) {
			    case PRINT_ALL: //print everything
			    	System.out.println(); //space
			    	movieList.printAll();
			    	System.out.println("\nPress ENTER to continue..");
			    	console.nextLine(); //wait for user..
			    	break;
			    case SEARCH: //search for movie
				    System.out.print("What movie are you looking for? >> ");
				    movieList.searchMovies(console.nextLine());
				    break;
			    case EXIT: //exit the program
			    	System.out.println("\nExiting the program..");
			    	System.exit(0);
			    	break;
			    default: //if any other cases arise, this is incorrect input
			    	System.out.println("Invalid Input");
		    }
		}
		else if(console.hasNext()) {
			System.out.println("Invalid Input");
		}
	}
	
	/*
	 * Method: storeMovies
	 * Purpose: parse the movie data from input file and save in db
	 * Parameters:
	 * 		Scanner fileScanner:	scanner of movie input file
	 * Returns: void
	 */
	
	public static void storeMovies(Scanner fileScanner) {
		while(fileScanner.hasNextLine()) {	//while there are more lines
			
			final int MOVIE_FIELDS = 4;
			String[] newMovie = new String[MOVIE_FIELDS];
			
			String line = fileScanner.nextLine(); //get the next line
			Scanner lineScanner = new Scanner(line); //most recent line scanner
			lineScanner.useDelimiter("/"); //using / as the delimiter
			
			int index = 0; //counter for while loop
			while(lineScanner.hasNext()) { //fill array with movie field data
				//try saving movie fields to array
				try {
					newMovie[index] = lineScanner.next();
				}
				//if there are more than 4 fields in file
				catch (ArrayIndexOutOfBoundsException exc){
					System.out.println("Movie File: found entry with " +
							"invalid number of fields.");
					System.exit(0);
				}
				
				index++;
			}
			
			//if there were fewer than 4 fields exit
			if(index < 3) {
				System.out.println("Movie with fewer than 4 fields found");
				System.exit(0);
 			}
			
			lineScanner.close();
			movieList.addMovie(newMovie); //add the movie to the database
		}
	}
	
	/*
	 * Method: storeShowings
	 * Purpose: parse the showing data from input file and save in db
	 * Parameters:
	 * 		Scanner fileScanner:	scanner of showing input file
	 * Returns: void
	 */
	
	public static void storeShowings(Scanner fileScanner) {
		while(fileScanner.hasNextLine()) {	//while there are more lines
			
			final int SHOWING_FIELDS = 3;
			String[] newShowing = new String[SHOWING_FIELDS];
			
			String line = fileScanner.nextLine(); //get the next line
			Scanner lineScanner = new Scanner(line); //most recent line scanner
			lineScanner.useDelimiter("/"); //using / as the delimiter
			
			int index = 0; //counter for while loop
			while(lineScanner.hasNext()) { //fill array with showing field data
				//try saving showing fields to array
				try {
					newShowing[index] = lineScanner.next();
				}
				//if there are more than 3 fields in file
				catch (ArrayIndexOutOfBoundsException exc){
					System.out.println("Showing File: found entry with " +
							"invalid number of fields.");
					System.exit(0);
				}
				
				index++;
			}
			
			//if there were fewer than 3 fields exit
			if(index < 2) {
				System.out.println("Showing with fewer than 3 " +
						"fields found");
				System.exit(0);
			}
			
			lineScanner.close(); //close scanner
			movieList.addShowing(newShowing); //add the movie to the database
		}
	}
}